package cn.kgc.web.controller;

import cn.kgc.base.entity.Tag;
import cn.kgc.base.utils.ResponseResult;
import cn.kgc.web.service.TagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/1/11
 * @description
 */
@RestController
@RequestMapping("tag")
public class TagController {

    @Resource
    private TagService tagService;

    // 1.标签列表
    @GetMapping
    public ResponseResult tagList(){

        List<Tag> list = tagService.list();
        return ResponseResult.success().data(list);
    }

    // 添加标签
    @PostMapping
    public ResponseResult  addTag(@RequestBody Tag tag){

        tag.setCreatedate(new Date());
        tagService.save(tag);

        return ResponseResult.success().data(tag).message("标签添加成功");
    }


    // 删除标签
    @DeleteMapping("{id}")
    public ResponseResult  removeTag(@PathVariable("id") Integer id){

        // TODO  在删除标签之前  判定标签是否被占用  student  clazz
        tagService.removeById(id);

        return ResponseResult.success().message("标签删除成功");
    }


    // 根据id  查询标签信息
    @GetMapping("{id}")
    public Tag  getTagById(@PathVariable("id") Integer id){

        Tag tag = tagService.getById(id);

        return tag;
    }

}
